define(["Zepto", 
        "underscore", 
        "Backbone", 
        "Handlebars",  
        "text!templates/teacher/new-notice.html",
        "models/avviso",
        "collections/corsocollection",
        "views/teacher/coursesinputview",
        "views/common/menuview"
], function($, _, Backbone, Handlebars, template, Avviso, CorsoCollection, CoursesInputView, MenuView) {
	
   
	  var NewNotice = Backbone.View.extend({

		  template : Handlebars.compile(template),

		  render: function(eventName) {
			  this.$el.html(this.template(this.model.toJSON()));
			  
			  // menu top
			  this.menutop = new MenuView({
				  el : $('#menu', this.el),
				  model : this.model
			  });
			  this.menutop.render();
	        	
			  
			  // stampa Id del professore
			  //alert('id ' + this.model.get('objectId'));
			  
			  this.corsoCollection = new CorsoCollection();
				this.corsoCollection.fetch({
					query : {
						docente: {"__type":"Pointer","className":"_User","objectId":this.model.get('objectId')}
	               	},
	               	success: function(data) {
	                	this.courses = new CoursesInputView({	    				
							el : $('#courses', this.el),
							model : data
	    				});
	                	this.courses.render();
					}});
				
			  return this;  
		  },

	
		  events: {
	            'click #insert': 'insertnotice',
	            'touchstart .button': 'over',
	            'touchend .button': 'left',
	            'touchcancel .button': 'left',
	            'touchstart button': 'over',
	            'touchend button': 'left',
	            'touchcancel button': 'left',
	            
	        },


	        insertnotice: function(event) {
	        	
	            var course_id = $('select.course').val();
	            var desc = $('textarea.big').val();
	            //var d = new Date();
	            
	            var notice = new Avviso({
	            	testo: desc, 
	            	//data: d.getDate(),
	            	corso: {"__type":"Pointer","className":"corso","objectId":course_id}
	            });
				notice.save({},{
					success: function(model, response) {
						alert('Avviso inserito');
					},
					error: function(model, response) {
						alert(response.responseText);
					},
				});
	            
	        },

	        over: function(event) {
	            $(event.currentTarget).addClass('fake-active');
	        },

	        left: function(event) {
	            $(event.currentTarget).removeClass('fake-active');
	        }
	    });

    return NewNotice;

});
